/*
 * SystemPropertyControl.java
 *
 * Created on 14 September 2004, 08:59
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.systemproperty;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.utility.InvalidKeyException;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Set;
import javax.ejb.CreateException;
import javax.naming.NamingException;

/**
 *
 * @author  Robert Crida Work
 */
public class SystemPropertyControl extends javax.swing.JPanel {
    
    private KeySession m_key;
    private SystemPropertyValueDisplay m_systemPropertyValueDisplay;
    
    /** Creates new form SystemPropertyControl */
    public SystemPropertyControl(KeySession p_key, Set p_setPermissions) {
        m_key = p_key;
        initComponents();
        m_systemPropertyValueDisplay = new SystemPropertyValueDisplay();
        jButtonEdit.setEnabled(p_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.SYSTEM_PROPERTY_EDIT))));
        updateSystemPropertyValueDisplay();
        add(m_systemPropertyValueDisplay, java.awt.BorderLayout.CENTER);
    }
    
    private void updateSystemPropertyValueDisplay() {
        try {
            Collection systemPropertyValues = StatelessRemoteClientSessionUtil.getSession().getAllSystemPropertyValues(m_key);
            m_systemPropertyValueDisplay.setSystemPropertyValues(systemPropertyValues);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jButtonEdit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonEdit.setText("Edit");
        jButtonEdit.setEnabled(false);
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonEdit);

        jButtonOK.setText("OK");
        jButtonOK.setEnabled(false);
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.setEnabled(false);
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        m_systemPropertyValueDisplay.setEditable(false);
        jButtonEdit.setEnabled(true);
        jButtonOK.setEnabled(false);
        jButtonCancel.setEnabled(false);
        updateSystemPropertyValueDisplay();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        try {
            StatelessRemoteClientSessionUtil.getSession().setSystemPropertyValues(m_key, m_systemPropertyValueDisplay.getSystemPropertyValues());
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        m_systemPropertyValueDisplay.setEditable(false);
        jButtonEdit.setEnabled(true);
        jButtonOK.setEnabled(false);
        jButtonCancel.setEnabled(false);
        updateSystemPropertyValueDisplay();                   
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
        // Add your handling code here:
        m_systemPropertyValueDisplay.setEditable(true);
        jButtonEdit.setEnabled(false);
        jButtonOK.setEnabled(true);
        jButtonCancel.setEnabled(true);        
    }//GEN-LAST:event_jButtonEditActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
}
